//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    baseURL: 'http://localhost/store/public/index/',
    array: [],
    index: 0,
    objectArray: []
  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', this.data.objectArray[e.detail.value.shool].schoolId)
    console.log(e.detail.value.snumber);
    if (e.detail.value.snumber == '' || e.detail.value.password == '') {
      wx.showToast({
        title: '请填写信息!',
        icon: 'none',
        duration: 2000
      });
    } else {
      wx.request({
        url: this.data.baseURL + 'User/register',
        method: 'POST',
        data: {
          'snumber': e.detail.value.snumber,
          'password': e.detail.value.password,
          'school': this.data.objectArray[e.detail.value.shool].schoolId,
          'teleNumber': e.detail.value.teleNumber
        },
        header: {

        },
        success: function (res) {
          switch (res.data) {
            case 1:
              wx.showToast({
                title: '注册成功',
                icon: 'success',
                duration: 1500
              });
              //TODO: 跳转到登录界面
              wx.navigateBack({
                delta: 1
              });
              break;
            case 2:
              wx.showToast({
                title: '学号不正确',
                icon: 'none',
                duration: 1500
              });
              break;
            case 3:
              wx.showToast({
                title: '学号已存在',
                icon: 'none',
                duration: 1500
              });
              break;
            default:
              wx.showToast({
                title: '注册失败',
                icon: 'none',
                duration: 1500
              });
          }
          console.log(res.data);
        }
      })
    }
  },
  formReset: function () {
    console.log('form发生了reset事件')
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', this.data.objectArray[e.detail.value])
    this.setData({
      index: e.detail.value
    })
  },
  onLoad: function () {
    console.log(app.globalData)
    var $this = this;
    wx.request({
      url: this.data.baseURL + 'Info/schoolInfo',
      method: 'POST',
      data: {},
      header: {

      },
      success: function (res) {
        console.log(res.data);
        $this.setData({
          objectArray: res.data
        })
      }
    })
  },
})